package com.zyg.jas.studentport.controller;

import com.zyg.jas.common.pojo.Course;
import com.zyg.jas.common.pojo.Homework;
import com.zyg.jas.common.pojo.Score;
import com.zyg.jas.common.pojo.Student;
import com.zyg.jas.studentport.service.HomeworkService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Controller
@RequestMapping("/sport/homework")
@CrossOrigin
public class HomeworkController2 {
    @Autowired
    private HomeworkService2 homeworkService;

    //根据courseid查询当前的homework该门课程全部具体信息
    @RequestMapping(value = "/findMyHomework/{courseId}/{comId}/{pageNo}/{pageSize}",method = RequestMethod.GET)
    @ResponseBody
    public List<Homework> findMyHomework(@PathVariable("courseId") String courseId,@PathVariable("comId") String comId,@PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize){
        List<Homework> homeworkList = this.homeworkService.getHomeworkByCourseid(courseId,comId,pageNo,pageSize);
        System.out.println(homeworkList);
        return homeworkList;
    }


    // 对应课程作业数量
    @RequestMapping(value = "count/{courseId}/{comId}",method = RequestMethod.GET)
    @ResponseBody
    public Integer getAllCount(@PathVariable("courseId") String courseId,@PathVariable("comId") String comId){
        return this.homeworkService.selectCount(courseId,comId);
    }

//
//    // 保存我的作业
//    @RequestMapping(value = "/saveHomework",method = RequestMethod.POST)
//    @ResponseBody
//    public Integer saveHomework(@RequestParam("resourceFile")MultipartFile multipartFile,
//                                       @RequestParam("hId") Integer hId, @RequestParam("sNo") String sNo,@RequestParam("comId") String comId,@RequestParam("commitName") String commitName) throws Exception {
//        Score score = new Score();
//        score.setComId(comId);
//        score.sethId(hId);
//        score.setsNo(sNo);
//        score.setCommitName(commitName);
////        Integer r = this.homeworkService.saveHomework(multipartFile,score);
//        try {
//            return this.homeworkService.saveHomework(multipartFile,score);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        return 0;
//    }


    // 保存作业
    @RequestMapping(value = "/saveJob",method = RequestMethod.POST)
    @ResponseBody
    public Integer saveJobHandler(@RequestParam("jobFile") MultipartFile multipartFile, @RequestParam("comId") String comId, @RequestParam("courseId")String courseId,
                                  @RequestParam("endDate") Date endDate, @RequestParam("mark") String mark) throws ParseException {
        Homework homework = new Homework();
        homework.setCourseId(courseId);
        homework.setComId(comId);
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        f.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String nowTime = f.format(endDate);
        Date date = f.parse(nowTime);
        Timestamp time = new Timestamp(date.getTime());
        homework.setEndDate(time);
        homework.setMark(mark);
        try {
            return this.homeworkService.saveJob(multipartFile,homework);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }




    // 保存作业
    @RequestMapping(value = "/saveJobForApp",method = RequestMethod.POST)
    @ResponseBody
    public Integer saveJobHandlerForApp(@RequestParam("jobFile") MultipartFile multipartFile, @RequestParam("fileName") String fileName,@RequestParam("comId") String comId, @RequestParam("courseId")String courseId,
                                  @RequestParam("endDate") Date endDate, @RequestParam("mark") String mark) throws ParseException {
        System.out.println("app接收到的作业");
        System.out.println(fileName);
        System.out.println(multipartFile.getOriginalFilename());
        Homework homework = new Homework();
        homework.setCourseId(courseId);
        homework.setComId(comId);
        homework.sethName(fileName); //add
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        f.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String nowTime = f.format(endDate);
        Date date = f.parse(nowTime);
        Timestamp time = new Timestamp(date.getTime());
        homework.setEndDate(time);
        homework.setMark(mark);
        try {
            return this.homeworkService.saveJobForApp(multipartFile,homework);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    // yi交作业
    @RequestMapping(value = "alreadyCommit/{sNo}",method = RequestMethod.GET)
    @ResponseBody
    public List<Homework> alreadyCommit(@PathVariable("sNo") String sNo){
        return this.homeworkService.selectAlreadyCommit(sNo);
    }

    // 未交作业
    @RequestMapping(value = "notCommit/{sNo}/{comId}",method = RequestMethod.GET)
    @ResponseBody
    public List<Homework> notCommit(@PathVariable("sNo") String sNo,@PathVariable("comId") String comId){
        return this.homeworkService.selectNotCommit(sNo,comId);
    }


    // 某个科目下的已交作业
    @RequestMapping(value = "specificAlreadyCommit/{sNo}/{courseId}",method = RequestMethod.GET)
    @ResponseBody
    public List<Homework> SpecificAlreadyCommit(@PathVariable("sNo") String sNo,@PathVariable("courseId") String courseId){
        return this.homeworkService.selectSpecificAlreadyCommit(sNo,courseId);
    }

    // 某个科目下的未交作业
    @RequestMapping(value = "specificNotCommit/{sNo}/{comId}/{courseId}",method = RequestMethod.GET)
    @ResponseBody
    public List<Homework> SpecificNotCommit(@PathVariable("sNo") String sNo,@PathVariable("comId") String comId,@PathVariable("courseId") String courseId){
        return this.homeworkService.selectSpecificNotCommit(sNo,comId,courseId);
    }

}
